/*
 * CHttpService.h
 *
 *  Created on: 2018年10月4日
 *      Author: zhengboyuan
 */

#ifndef CHTTPSERVICE_H_
#define CHTTPSERVICE_H_

#include "BasicType.h"
#include <string>

#include "HttpServer.h"
#undef snprintf

#include "RestHttpHandler.h"

#include "PersonFlowCounter.h"
#include "SharedPtr.h"


class CHttpService : public RestHttpHandler
{
public:
    CHttpService();
    virtual ~CHttpService();

	void setCounter(PersonFlowCounter* counter);

	virtual bool start(int port);

	virtual void stop();

	virtual bool isStarted();

	virtual void handleUpload(const char* name, const std::string& filepath);

	virtual int handleFileApi(HttpRequest& request,
		std::string& resp, std::string& mime);

	virtual void handleWebSocketFrame(unsigned char *data, size_t size, unsigned char flags);

	void setFlowCount(const PersonFlowCount& flowCount);

	PersonFlowCount getFlowCount();


protected:
	static std::string getWebsitePath();

	void setupUri();

	int fetchFlowCount(HttpRequest& request, std::string& text);
	int fetchFlowCountPart(HttpRequest& request, std::string& text);
	
	std::string makeRss(const std::string& title, const std::string& description);

	std::string makeRss(const std::string& id, const std::string& name, const PersonFlowCount& flowCount,
		const std::string& enterName, const std::string& leaveName, const std::string& onlineName,
		const std::string& sp, char holder, int roll);

	std::string formatCount(const PersonFlowCount& flowCount,
		const std::string& enterName, const std::string& leaveName,
		const std::string& onlineName, const std::string& sp, char holder);

	void loadConfig();

	std::string formatNum(int num, char holder);

	
protected:
	PersonFlowCounter*	m_counter;

	std::shared_ptr<HttpServer>     m_server;
	int	m_port;

	std::string	m_enterName;
	std::string	m_leaveName;
	std::string	m_onlineName;
	std::string	m_seperator;

	int	m_count;

	comn::CriticalSection	m_csFlow;
	PersonFlowCount	m_flowCount;

};

#endif /* CHTTPSERVICE_H_ */
